<!--
@Description:底部公用TabBar导航标签
@Author:zqy
@Date:2022-10-21 10:47:21
@Last Modified by:zqy
@Last Modified time:2022-10-21 10:47:21
-->
<template>
  <div class="bottom-tabbar">
    <van-tabbar v-model="activeNav" active-color="#16BC83" inactive-color="#B5B7C4" fixed safe-area-inset-bottom>
      <van-tabbar-item replace to="/">
        <span>客户管理</span>
        <template #icon="props">
          <img :src="props.active ? iconCustomer.active : iconCustomer.inactive">
        </template>
      </van-tabbar-item>
      <van-tabbar-item replace to="/purorder">
        <span>订单采购</span>
        <template #icon="props">
          <img :src="props.active ? iconPur.active : iconPur.inactive">
        </template>
      </van-tabbar-item>
      <div v-if="showPlusbtn" class="nav-plus" @click.stop="toAddCustomer"><span class="icon-plus" /></div>
    </van-tabbar>
    <Popup v-model="showCreate" title="选择新建客户类型" :show-cancel-button="false" :show-confirm-button="false">
      <div class="pop-create-wrap" @click.stop="handleCreate(1)">
        <div class="item-content" :class="{'active':customerType!==2}">
          <div class="icon">
            <img src="~@/assets/nav/customer-t1.png">
          </div>
          <div class="name">潜在客户</div>
        </div>
        <div class="item-content" :class="{'active':customerType===2}" @click.stop="handleCreate(2)">
          <div class="icon">
            <img src="~@/assets/nav/customer-t2.png">
          </div>
          <div class="name">意向客户</div>
        </div>
      </div>
    </Popup>
  </div>
</template>

<script>
import Popup from '@/components/Popup'
export default {
  name: 'Tabbar',
  components: { Popup },
  props: {
    value: {
      type: Number,
      default: 0
    },
    customerType: {
      type: Number,
      default: 1
    }// 当前所在的页面客户类型：1潜客  2 意向客户
  },
  data() {
    return {
      iconCustomer: {
        active: require('@/assets/nav/icon-customer-active.png'),
        inactive: require('@/assets/nav/icon-customer.png')
      },
      /* iconCart: {
        active: require('@/assets/nav/icon-nav-cart-active.png'),
        inactive: require('@/assets/nav/icon-nav-cart.png')
      }, */
      iconPur: {
        active: require('@/assets/nav/icon-pur-active.png'),
        inactive: require('@/assets/nav/icon-pur.png')
      },
      activeNav: 0,
      showCreate: false
    }
  },
  computed: {
    // 新增客户按钮 (当前职务为：导购 top_shopping_guide ，店长 top_shop_mg 时)
    showPlusbtn() {
      const t = this.$store.getters.userInfo
      if (t.positionCodes && t.positionCodes.length > 0) {
        return t.positionCodes.some(v => ['top_shopping_guide', 'top_shop_mg'].includes(v))
      }
      return false
    },
    // 只有店长权限，店长只能新增潜客
    isShopMg() {
      if (!this.showPlusbtn) return
      const t = this.$store.getters.userInfo
      return !t.positionCodes.some(v => v === 'top_shopping_guide')
    }
  },
  watch: {
    value: {
      handler(v) {
        this.activeNav = v
      },
      immediate: true
    },
    activeNav(v) {
      this.$emit('input', v)
    },
    $route: {
      handler(route, oldval) {
        const tabs = ['CustomerHome', 'PurOrderList']
        const index = tabs.indexOf(route.name)
        this.activeNav = index !== -1 ? index : 0
        // this.showCreate = false
      },
      immediate: true,
      deep: true
    }
  },
  created() {

  },
  methods: {
    toAddCustomer() {
      // 意向客户页签点新增，进入新增意向客户页面
      // 其他页签点新增，进入新增潜客页面
      /*  this.$router.push({
        name: 'CustomerAdd',
        query: { type: this.customerType === 2 ? 2 : 1 }
      }) */
      if (this.isShopMg) {
        this.$router.push({
          name: 'CustomerAdd',
          query: { type: 1 }
        })
        return
      }
      this.showCreate = true
    },
    handleCreate(type) {
      this.$emit('change-customer', type)
      this.showCreate = false
      this.$nextTick(() => {
        this.$router.push({
          name: 'CustomerAdd',
          query: { type: type === 2 ? 2 : 1 }
        })
      })
    }
  }
}

</script>
<style lang='less' scoped>
.bottom-tabbar{
  position: relative;
  // height: 50px;
  height: 80px;
  /deep/ .van-tabbar{
    box-shadow: 0px -1px 10px 0px rgba(0,0,0,0.1);
  }
  /deep/ .van-tabbar-item{
    font-size: 10px;
  }
  /deep/ .van-tabbar-item__icon img{
    height: 26px;
  }
  .nav-plus{
    position: absolute;
    z-index: 9999;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    width: 61px;
    height: 61px;
    display: flex;
    justify-content: center;
    align-items: center;
    box-sizing: border-box;
    background: #e2e2e2;
    border-radius: 50%;
    box-shadow: 0px 0px 30px 0px rgba(0,0,0,0.12);
    .icon-plus{
      display: inline-block;
      width: 55px;
      height: 55px;
      background: #16BC83;
      border-radius: 50%;
      &::before,&::after{
        content: '';
        background: #fff;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
        border-radius: 2px;
      }
      &::before{
        width: 4px;
        height: 19px;
      }
      &::after{
        width: 19px;
        height: 4px;
      }
    }
  }
}
.pop-create-wrap{
  display: flex;
  justify-content: space-between;
  padding-bottom: 30px;
  .item-content{
    width: calc(50% - 8px);
    height: 146px;
    border-radius: 12px;
    background: #fff;
    border: 2px solid #B5B7C4;
    color: #A8B0C5;
    &.active{
      border-color: #16BC83;
      background: rgba(22,188,131,0.08);
      color: #16BC83;
    }
    .icon{
      width: 68px;
      height: 68px;
      margin:24px auto 20px;
      img{
        width: 68px;
        height: 68px;
      }
    }
    .name{
      height: 20px;
      font-size: 14px;
      font-weight: bold;
      line-height: 20px;
      text-align: center;
    }
  }
}
</style>
